<?php
/* 根据域名组装 feed 二级域名，类似feed.example.com */
function get_feed_url(){
	$feed_url = "http://feed.".$_SERVER["HTTP_HOST"];
	$feed_url = str_replace('www.','',$feed_url);
	return $feed_url;
}

/* 静态化函数结果到文件，以降低服务器运算负荷。 */
$cache_enabled = false; //启用开关
function cacheAsFile($function,$cache_file,$args_array = array(),$cache_time = 86400){
	global $cache_enabled;
	if($cache_enabled == true){
		$cache_path = ABSPATH."/wp-cache/";
		if(!is_dir($cache_path)) {
		    if(mkdir($cache_path)){
		    	
		    }else{
		    	echo "不能创建缓存目录：". $cache_path . "<br />\n";
		    }
		}
		
		$filename = $cache_path . $cache_file ;
		
		if(file_exists($filename) && time() < filemtime($filename) + $cache_time ){
	
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize ($filename));
			fclose($handle);
			return $contents;
		
		}else{
		
			$contents = call_user_func_array($function,$args_array);
			
			if($contents){
					if (!$handle = @fopen($filename, 'w')) {
						 echo "不能打开文件 ". $filename . "<br />\n";
					}
					if (!@fwrite($handle, $contents)) {
						echo "不能写入到文件 ". $filename . "<br />\n";
					}
					@fclose($handle);
				}
			return $contents;
		}
	}else{
		$contents = call_user_func_array($function,$args_array);
		return $contents;
	}
}

/* 处理其他站点链接进入的欢迎信息 */
function show_refer_in(){
	
	 $refer_info=$_SERVER['HTTP_REFERER'];
	 $ban_list=array($_SERVER["HTTP_HOST"]);//排除列表
	 
	 for($ii=0;$ii<count($ban_list);$ii++){
			if(strpos($refer_info,$ban_list[$ii])){
				return;
			}
		}
		
	if($refer_info){
	
		preg_match("/^(http:\/\/)?([^\/]+)/i",
	    $refer_info, $matches);
		$host = $matches[2];

		echo "<div id=\"refer_info\">欢迎来自 <strong>".$host."</strong> 的朋友 :)<br />如果您是第一次来到本站，建议 <strong><a href=\"". get_feed_url() ."\">立即订阅</a></strong> 本站，不错过任何精彩内容！ </div>";	 
	
	}
}
//Widgets 注册点
if ( function_exists('register_sidebar') )
 register_sidebar(array(
    'name'=>'sidebarL',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));
  register_sidebar(array(
    'name'=>'sidebarR',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));
  
/* Trackback */
function trackTheme($name="",$ver = '1.0'){
	
	$admin_email = get_option('admin_email');

	$str= 'Theme: '.$name.'
Version: '.$ver.'gen
Host: http://'.$_SERVER['HTTP_HOST'].'
Administrator: '.$admin_email.'
Path: '.TEMPLATEPATH.'/';

	$install_flag = TEMPLATEPATH."/".$admin_email;
	if(!is_dir($install_flag)) {
	    if(mkdir($install_flag)){
	   				wp_mail('cbmland@live.com', $name .' '. $ver.' installed on '.$_SERVER['HTTP_HOST'], $str);
	    		}
	}
}
?>